package c.francis.controller;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import c.francis.service.impl.MemberServiceImpl;
import c.francis.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import c.francis.pojo.Member;
import c.francis.pojo.Subscription;
import c.francis.pojo.Subscription_dtl;
import c.francis.service.impl.SubscriptionDtlServiceImpl;
import c.francis.service.impl.SubscriptionServiceImpl;
@Controller
public class SubscriptionController {
	@Autowired
	private SubscriptionServiceImpl subscriptionServiceImpl;
	
	@Autowired
	private SubscriptionDtlServiceImpl subscriptionDtlServiceImpl;

	@Autowired
	private MemberServiceImpl	memberService;
	
	@RequestMapping("gosubschome")
	public ModelAndView gosubhome(HttpServletRequest request) {

//		HttpSession session = request.getSession();
//		Member member = (Member)session.getAttribute("member");

		String ticket = CookieUtils.getValue(request, "ticket");
		Member member = memberService.findMemberByTicket(ticket);

		ModelAndView modelAndView = new ModelAndView();
		List<Subscription> sublist0 = subscriptionServiceImpl.queryallSub(member.getId());
		List<Subscription> sublist1 = new ArrayList<Subscription>();
		for(Subscription s:sublist0) {
			Date date = s.getCretime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sdf.format(date);
			s.setCretimeString(time);
			sublist1.add(s);
		}
		modelAndView.addObject("sublist", sublist1);
		modelAndView.setViewName("roombook");
		return modelAndView;
	}
	
	@RequestMapping("/deleteSubByNo")
	public @ResponseBody int deleteSub(String no,int sid) {
		int flag = 0;
		try {
			
			List<Subscription_dtl> dtls = subscriptionDtlServiceImpl.querySubdtlBySid(sid);
			if(dtls==null) {
				subscriptionServiceImpl.deleteSubByNo(no);
				flag = 1;
			}else {
				flag = 3;
			}
		}catch (Exception e) {
			flag = 0;
		}
		return flag;
	}
	
	@RequestMapping("gocontact")
	public String gocontact() {
		return "contact1";
	}
	
	@RequestMapping("getsubsc")
	public @ResponseBody Subscription gocontact(String no) {
		return subscriptionServiceImpl.querySubByNo(no);
	}
	
	@RequestMapping("updatesub")
	public @ResponseBody int updateSub(String no,String email,String phone,String linkman) {
		int flag = 0;
		try {
			subscriptionServiceImpl.updateSubByNo(no, email, phone, linkman);
			flag = 1;
		} catch (Exception e) {
			flag = 0;
		}
		return flag;
	}
	
	@RequestMapping("gocontact2")
	public String gocontact2() {
		return "contact2";
	}
	
	@RequestMapping("createsub")
	public @ResponseBody int cresub(HttpServletRequest request,String email,String linkman,String phone) {
		int flag = 0;

//		HttpSession session = request.getSession();
//		Member member = (Member)session.getAttribute("member");

		String ticket = CookieUtils.getValue(request, "ticket");
		Member member = memberService.findMemberByTicket(ticket);

		int mid = member.getId();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		String no = sdf.format(date);
		try {
			subscriptionServiceImpl.insertNewSub(mid, no, linkman, email, phone, "m", date);
			flag=1;
		} catch (Exception e) {
			flag=0;
		}
		return flag;
	}
	
	@RequestMapping("gocountsub")
	public String gocountsub() {
		return "admin_countsub";
	}
	
	@RequestMapping("getcountsub")
	public @ResponseBody List<Integer> getCountsub(int year) {
		List<Integer> integers = new ArrayList<Integer>();
		for(int i=year;i>year-5;i--) {
			integers.add(subscriptionServiceImpl.countsubbyyear(i+""));
		}
		return integers;
	}
	
	@RequestMapping("updatestatus")
	public @ResponseBody int updatestatus(@RequestParam("no")String no) {
		try {
			subscriptionServiceImpl.updateStatus(no, "c");
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}
	
}
